<div class="form">
    
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'content-form',
	'enableAjaxValidation'=>true,
)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model,'title'); ?>
		<?php echo $form->textField($model,'title'); ?>
		<?php echo $form->error($model,'title'); ?>
    </div>
    
    <div class="row">
        <?php
            $this->widget('ext.redactor.ERedactorWidget',array(
                'model'=>$model,
                'attribute'=>'text',
                'options'=>array(
                    'fileUpload'=>Yii::app()->createUrl('content/fileUpload',array(
                        'attr'=>'text'
                    )),
                    'fileUploadErrorCallback'=>new CJavaScriptExpression(
                        'function(obj,json) { alert(json.error); }'
                    ),
                    'imageUpload'=>Yii::app()->createUrl('content/imageUpload',array(
                        'attr'=>'text'
                    )),
                    'imageGetJson'=>Yii::app()->createUrl('content/imageList',array(
                        'attr'=>'text'
                    )),
                    'imageUploadErrorCallback'=>new CJavaScriptExpression(
                        'function(obj,json) { alert(json.error); }'
                    ),
                ),
            ));
        ?>
    </div>
    
    <div class="row">
		<?php echo $form->labelEx($model,'explain'); ?>
		<?php echo $form->textArea($model,'explain'); ?>
		<?php echo $form->error($model,'explain'); ?>
    </div>

    <div class="row">
        <input class="text_input" type="text"/>
        <button class="submit_text">
            提交
        </button>
    </div>
    <ul class="choice_wrap">
        <?php if( ! $model->getIsNewRecord() && ! empty($model->choices)):?>
            <?php foreach($model->choices as $choice):?>
                <?php if($choice instanceof Choice):?>
                <li>
                    <input type="hidden" name="Choice[id]" value="<?php echo $choice->getId();?>"/>
                    <input type="radio" name="Choice[answer]" <?php if((bool)($choice->answer)):?>checked="checked"<?php endif;?>/>
                    <input type="text" name="Choice[text]" value="<?php echo $choice->getDisplayText();?>"/>
                    <a href="javascript:;" class="cancel">x</a>
                </li>
                <?php endif;?>
            <?php endforeach;;?>
        <?php endif;?>
    </ul>
    
	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->
<script type="text/javascript">
$(document).ready(function(){
    $(document).on('click', '.submit_text', function(){
        var input = $('.text_input');
        var text = $.trim(input.val());
        
        if( ! text)
        {
            alert('选择不能为空.');
            return false;
        };
        
        var html = '';
        html += '<li>';
        html += '<input type="hidden" name="Choice[id]"/>';
        html += '<input type="radio" name="Choice[answer]"/>';
        html += '<input type="text" name="Choice[text]" value="'+text+'"/>';
        html += '<a href="javascript:;" class="cancel">x</a>';
        html += '</li>';
       
       $('.choice_wrap').append(html);
       input.val('');
        return false;
    });
    
    $(document).on('click', '.cancel', function(){
        $(this).parent().remove();
        return false;
    });
    
    $(document).on('submit', '#content-form', function(){
        var data = {};
        var self = this;
        var oneChoiceData = $(this).serializeObject().OneChoice;

        var choices = [];
        
        if( 1 > $('.choice_wrap > li').length)
        {
            alert('请添加选项.');
            return false;
        };
        
        if( 1 > $('.choice_wrap > li').find(':radio[checked="checked"]').length)
        {
            alert('至少选择一个');
            return false;
        };
        
        $('.choice_wrap > li').each(function(){
            choices.push($(this).find('[name]').serializeObject().Choice);
        });

        if(0 < choices.length)
        {
            oneChoiceData.choices = choices;
        };
        
        $.ajax({
            url: $(self).attr('action'),
            type: 'post',
            data: {OneChoice: oneChoiceData},
            dataType: 'json',
            success: function(json){
                if(json.result && !! json.data)
                    window.location.href = json.data.url;
            },
            error: function(xrt, errors, d){
                alert('save error');
                return false;
            }
        });
        return false;
    });
});
</script>